package com.daigoudao.web.action;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.daigoudao.config.ConfigManager;
import com.daigoudao.util.Constants;
import com.daigoudao.util.FileUtil;
import com.daigoudao.util.StringUtils;
import com.daigoudao.web.common.BaseAction;
import com.daigoudao.web.model.AreaModel;
import com.daigoudao.web.model.CanBuyModel;
import com.daigoudao.web.model.CategoryModel;
import com.daigoudao.web.model.InviteModel;
import com.daigoudao.web.model.UserModel;
import com.daigoudao.web.model.WantBuyModel;
import com.daigoudao.web.service.ICanBuyService;
import com.daigoudao.web.service.ICommonService;
import com.daigoudao.web.service.IInviteService;
import com.daigoudao.web.service.IWantBuyService;

public class InviteAction extends BaseAction {

	private static final long serialVersionUID = -3171730287843181139L;

	private static Logger logger = LoggerFactory.getLogger(InviteAction.class);

	private IInviteService inviteService;

	private ICommonService commonService;

	private IWantBuyService wantBuyService;

	private ICanBuyService canBuyService;

	/**
	 * 代购邀约信息
	 */
	private InviteModel inviteModel;

	private UserModel userModel;

	/**
	 * 代购信息
	 */
	private CanBuyModel canBuyModel;

	private WantBuyModel wantBuyModel;

	private List<CategoryModel> categorys;

	private List<AreaModel> areas;

	private String successMsg;

	/**
	 * 返回URL
	 */
	private String backUrl;

	/**
	 * 上传的文件
	 */
	private List<File> image;

	/**
	 * 上传的文件名称
	 */
	private List<String> imageFileName;

	private List<String> imageContentType;

	private List<CanBuyModel> canBuyModels;

	/**
	 * 跳转至代购邀约页面
	 * 
	 * @return
	 */
	public String invite() {
		if (0 != id) {
			wantBuyModel = new WantBuyModel();
			wantBuyModel.setWantId(id);
		}
		UserModel loginModel = getUserLogin();
		if (loginModel == null) {
			return CERTIFICATION;
		}
		if (Constants.USER_TYPE_BROKER != loginModel.getUserType()) {
			return CERTIFICATION;
		}
		try {
			wantBuyModel = wantBuyService.queryWantBuyDetail(wantBuyModel
					.getWantId());
			categorys = commonService.queryCategorys();
			areas = commonService.queryAreas();
			CanBuyModel queryModel = new CanBuyModel();
			queryModel.setCategoryId(wantBuyModel.getCategoryId());
			queryModel.getUser().setUserid(loginModel.getUserid());
			queryModel.setStatus(0);
			queryModel.setPageSize(0);
			canBuyModels = canBuyService.queryCanBuys(queryModel);
		} catch (Exception e) {
			logger.error("invite error.", e);
			return ERROR;
		}
		return SUCCESS;
	}

	/**
	 * 确认邀约
	 * 
	 * @return
	 */
	public String confirmInvite() {
		try {
			if (image != null) {
				String root = ConfigManager.getConfigString("upload.resDir");
				String path = StringUtils.formatDate(new Date(), "yyyy-MM-dd");
				String basePath = root + File.separator + path;
				FileUtil.createDir(basePath);
				List<String> images = new ArrayList<String>();
				// 循环上传的文件
				for (int i = 0; i < image.size(); i++) {
					// 获取文件扩展名
					String fileName = imageFileName.get(i);
					String extName = FileUtil.getExtention(fileName);

					fileName = StringUtils.getUUID() + extName;
					File destFile = new File(basePath, fileName);

					// 文件复制
					try {
						FileUtil.copy(image.get(i), destFile);
					} catch (Exception e) {
					}

					// 文件在数据库存储
					String urlPath = File.separator + path + File.separator
							+ fileName;
					// 保存到数据库
					images.add(urlPath);
					canBuyModel.setImages(images);
				}
			}
			inviteService.addInviteInfo(wantBuyModel.getWantId(), canBuyModel);
		} catch (Exception e) {
			logger.error("confirmInvite error.", e);
			return ERROR;
		}
		successMsg = "您已成功发起邀约消息,请等待买家接受！";
		backUrl = "wantBuyList.htm";
		return SUCCESS;
	}

	/**
	 * 判断能否邀约(已邀约过）
	 * 
	 * @return
	 */
	public String canInvite() {
		inviteModel.setCanUserId(getUserLogin().getUserid());
		try {
			int count = inviteService.getInviteCount(inviteModel);
			if (count > 0) {
				ajaxMsg = "不能重复邀约";
			}
		} catch (Exception e) {
			logger.error("get invite count error.", e);
			ajaxMsg = "系统忙，请稍候再试";
		}
		return SUCCESS;
	}

	public InviteModel getInviteModel() {
		return inviteModel;
	}

	public void setInviteModel(InviteModel inviteModel) {
		this.inviteModel = inviteModel;
	}

	public UserModel getUserModel() {
		return userModel;
	}

	public void setUserModel(UserModel userModel) {
		this.userModel = userModel;
	}

	public CanBuyModel getCanBuyModel() {
		return canBuyModel;
	}

	public void setCanBuyModel(CanBuyModel canBuyModel) {
		this.canBuyModel = canBuyModel;
	}

	public WantBuyModel getWantBuyModel() {
		return wantBuyModel;
	}

	public void setWantBuyModel(WantBuyModel wantBuyModel) {
		this.wantBuyModel = wantBuyModel;
	}

	public List<CategoryModel> getCategorys() {
		return categorys;
	}

	public void setCategorys(List<CategoryModel> categorys) {
		this.categorys = categorys;
	}

	public List<AreaModel> getAreas() {
		return areas;
	}

	public void setAreas(List<AreaModel> areas) {
		this.areas = areas;
	}

	public String getSuccessMsg() {
		return successMsg;
	}

	public void setSuccessMsg(String successMsg) {
		this.successMsg = successMsg;
	}

	public String getBackUrl() {
		return backUrl;
	}

	public void setBackUrl(String backUrl) {
		this.backUrl = backUrl;
	}

	public void setInviteService(IInviteService inviteService) {
		this.inviteService = inviteService;
	}

	public void setCommonService(ICommonService commonService) {
		this.commonService = commonService;
	}

	public List<File> getImage() {
		return image;
	}

	public void setImage(List<File> image) {
		this.image = image;
	}

	public List<String> getImageFileName() {
		return imageFileName;
	}

	public void setImageFileName(List<String> imageFileName) {
		this.imageFileName = imageFileName;
	}

	public List<String> getImageContentType() {
		return imageContentType;
	}

	public void setImageContentType(List<String> imageContentType) {
		this.imageContentType = imageContentType;
	}

	public void setWantBuyService(IWantBuyService wantBuyService) {
		this.wantBuyService = wantBuyService;
	}

	public List<CanBuyModel> getCanBuyModels() {
		return canBuyModels;
	}

	public void setCanBuyModels(List<CanBuyModel> canBuyModels) {
		this.canBuyModels = canBuyModels;
	}

	public void setCanBuyService(ICanBuyService canBuyService) {
		this.canBuyService = canBuyService;
	}
}
